package com.find.memories.service;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;

public class DBHelper extends SQLiteOpenHelper {
	public static final String DB_NAME = "findmemories.db";
	public static final int DB_VERSION = 1; 

	static DBHelper instance;

	public DBHelper(Context context) {
		super(context, DB_NAME, null, DB_VERSION);
	}

	public static SQLiteDatabase getInstance(Context ctx) {
		if (instance == null)
			instance = new DBHelper(ctx);
		return instance.getWritableDatabase();
	}
	
	public static SQLiteDatabase getReadableInstance(Context ctx)
	{
		if(instance == null)
			instance = new DBHelper(ctx);
		return instance.getReadableDatabase();
	}

			
	private static final String MYLOCATION_LOG = "CREATE TABLE MYLOCATION_LOG (" 
			+ "_id INTEGER PRIMARY KEY AUTOINCREMENT,"
			+ "REG_DATE TIMESTAMP,"
			+ "LATITUDE VARCHAR2(30),"
			+ "LONGITUDE VARCHAR2(30));";

	@Override
	public void onCreate(SQLiteDatabase db) {
		try {
			db.execSQL(MYLOCATION_LOG);
			
		} catch (SQLiteException ex) {
			ex.printStackTrace();
		}
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		try {
			db.execSQL("DROP TABLE IF EXISTS KDN_MYLOCATION;");
		} catch (SQLiteException ex) {
			ex.printStackTrace();
		}
		onCreate(db);
	}

}
